package com.ruoyi.patient.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.patient.entity.LoginPatient;
import com.ruoyi.patient.entity.AddressBody;
import com.ruoyi.patient.service.PatientTokenService;
import com.ruoyi.patient.service.AddressService;
import com.ruoyi.patient.utils.PatientTokenFilter;
import com.ruoyi.service.domain.ServiceAddress;
import com.ruoyi.service.service.IServiceAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 服务地址控制类
 *
 * @author ZDL
 */

@RestController
@RequestMapping("/appPatient/appPatientAddress")
public class AddressController {
    
    @Autowired
    private IServiceAddressService serviceAddressService;

    @Autowired
    private AddressService addressService;

    @Autowired
    private PatientTokenService tokenService;

    @Autowired
    private PatientTokenFilter patientTokenFilter;

    
    /**
     * 服务地址新增方法
     *
     * @param addressBody 服务地址操作信息
     * @return 结果
     */
    @PostMapping("/addAddress")
    public AjaxResult addServesd(@RequestBody AddressBody addressBody, HttpServletRequest request) {
        try {
            if(!patientTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            if (!"0".equals(addressBody.getFlag())) {
                return AjaxResult.warn("操作无效");
            }
            if(addressBody.getAddressInfo() == null || addressBody.getAddressDetail() == null || addressBody.getAddressPeople() == null || addressBody.getAddressPhone() == null) {
                return AjaxResult.warn("信息错误");
            }
            LoginPatient oldPatient = tokenService.getLoginUser(request);
            if (oldPatient.getUserId() == null || oldPatient.getUserId() != addressBody.getAddressUserid()) {
                return AjaxResult.warn("用户信息错误");
            }
            if (addressService.addAddress(addressBody, oldPatient)) {
                return AjaxResult.success("添加成功");
            } else {
                return AjaxResult.error("添加失败");
            }

        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 服务地址修改方法
     *
     * @param addressBody 服务地址操作信息
     * @return 结果
     */
    @PostMapping("/updateAddress")
    public AjaxResult updateAddress(@RequestBody AddressBody addressBody, HttpServletRequest request) {
        try {
            if(!patientTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            if (!"1".equals(addressBody.getFlag())) {
                return AjaxResult.warn("操作无效");
            }
            if(addressBody.getAddressInfo() == null || addressBody.getAddressDetail() == null || addressBody.getAddressPeople() == null || addressBody.getAddressPhone() == null) {
                return AjaxResult.warn("信息错误");
            }
            LoginPatient oldPatient = tokenService.getLoginUser(request);
            if (oldPatient.getUserId() == null || oldPatient.getUserId() != addressBody.getAddressUserid()) {
                return AjaxResult.warn("用户信息错误");
            }
            ServiceAddress oldAddress = serviceAddressService.selectServiceAddressByAddressId(addressBody.getAddressId());
            if(oldAddress == null || oldAddress.getAddressUserid() != addressBody.getAddressUserid()){
                return AjaxResult.warn("服务地址信息错误");
            }else{
                if (addressService.updateAddress(addressBody, oldPatient)) {
                    return AjaxResult.success("修改成功");
                } else {
                    return AjaxResult.error("修改失败");
                }
            }

        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 服务地址删除方法
     *
     * @param addressBody 服务地址操作信息
     * @return 结果
     */
    @PostMapping("/deleteAddress")
    public AjaxResult deleteAddress(@RequestBody AddressBody addressBody, HttpServletRequest request) {
        try{
            if(!patientTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            if (!"2".equals(addressBody.getFlag())) {
                return AjaxResult.warn("操作无效");
            }
            if(addressBody.getAddressId() == null){
                return AjaxResult.warn("信息错误");
            }
            LoginPatient oldPatient = tokenService.getLoginUser(request);
            if (oldPatient.getUserId() == null || oldPatient.getUserId() != addressBody.getAddressUserid()) {
                return AjaxResult.warn("用户信息错误");
            }
            ServiceAddress oldAddress = serviceAddressService.selectServiceAddressByAddressId(addressBody.getAddressId());
            if(oldAddress == null || oldAddress.getAddressUserid() != addressBody.getAddressUserid()){
                return AjaxResult.warn("服务地址信息错误");
            }else{
                addressBody.setAddressDelFlag("2");
                if (addressService.updateAddress(addressBody, oldPatient)) {
                    return AjaxResult.success("删除成功");
                } else {
                    return AjaxResult.error("删除失败");
                }
            }

        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 服务地址查询方法
     *
     * @param addressBody 服务地址操作信息
     * @return 结果
     */
    @GetMapping("/getAddress")
    public AjaxResult getAddress(@RequestBody AddressBody addressBody, HttpServletRequest request) {
        try {
            if(!patientTokenFilter.doFilterInternal(request)) {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("code", 401);
                ajaxResult.put("msg", "用户信息过期，请重新登录");
                return ajaxResult;
            }
            if (!"3".equals(addressBody.getFlag())) {
                return AjaxResult.warn("操作无效");
            }
            LoginPatient oldPatient = tokenService.getLoginUser(request);
            if (oldPatient.getUserId() == null || oldPatient.getUserId() != addressBody.getAddressUserid()) {
                return AjaxResult.warn("用户信息错误");
            }
            addressBody.setAddressDelFlag("0");
            List<ServiceAddress> addressList = addressService.getAddress(addressBody, oldPatient);
            return AjaxResult.success("获取成功", addressList);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

}
